import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-06-5
 * Time: 19:53
 */
public class TestDemo {
    /**
     * 计数排序
     */
    public static void countSort(int[] array) {
        int max = array[0];
        int min = array[0];

        //找最大值最小值确定数组的范围
        for (int i = 0; i < array.length; i++) {
            if(max < array[i]) {
                max = array[i];
            }

            if(min > array[i]) {
                min = array[i];
            }
        }
        //确定范围以后开始计数
        int[] ret = new int[max - min + 1];
        int len = max - min + 1;

        for (int i = 0; i < array.length; i++) {
            ret[array[i]-min]++;
        }

        //通过计数数组下标处的元素确定该元素有几个，然后打印
        //因为本身在放入的时候是减去了一个最小值，所有在放入的时候加上最小值就是原数组的值
        int index = 0;
        for (int i = 0; i < len; i++) {

            while(ret[i] > 0) {
                array[index++] = i+min;
                ret[i]--;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {10,22,3,88,12,66,44,2,1,99,2,1,88};
        countSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main3(String[] args) {
        double a = Integer.parseInt("123",16);
        System.out.println(a);
    }
    public static void main2(String[] args) {
        System.out.println(Integer.toBinaryString(123));
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5,6};
        System.out.println(array.length);
    }
}
